package com.jiang.daily.Q1578;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/11/03 11:35
 */
class Solution {
    public int minCost(String colors, int[] neededTime) {
        char[] color = colors.toCharArray();
        int len = colors.length();
        int cost = 0;

        for (int i = 1, j = 1; i < len; i = j) {
            if (color[i] == color[i - 1]) {
                int maxValue = neededTime[i - 1];
                for (; j < len; j++) {
                    if (color[j] == color[j - 1]) {
                        cost += Math.min(neededTime[j], maxValue);
                        maxValue = Math.max(neededTime[j], maxValue);
                    } else {
                        break;
                    }
                }
            }
            j++;
        }
        return cost;
    }
}
